package model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

public class Wall{
	private int startPosX,startPosY,endPosX,endPosY;
	private Color color;
	 	
	// constructor
	public Wall(int startPosX,int startPosY,int endPosX,int endPosY) {
		this.startPosX = startPosX;
		this.startPosY = startPosY;
		this.endPosX = endPosX;
		this.endPosY= endPosY;

		color = new Color(255,255,0);
	}

	// returns true if the line (start;end) passes through the wall
	public boolean isTriyingToPassThrough(int startX,int startY,int endX,int endY,boolean movesHorizontally){
		if(movesHorizontally && crossesHorizontally(startX,startY,endX,endY))
			return true;
		
		else if ((!movesHorizontally) && crossesVertically(startX,startY,endX,endY))
			return true;
		
		return false;
	}
	
	// returns true if the line (start;end) passes through the wall
	// from left to right or right to left
	private boolean crossesVertically(int startX,int startY,int endX,int endY){		
		if(isInTheMiddleHorizontally(startX))
			if((isAbove(startY) && isBelow(endY))||(isAbove(endY) && isBelow(startY)))
				return true;
		return false;
	}
	
	// returns true if the line (start;end) passes through the wall
	// upwards or downwards
	private boolean crossesHorizontally(int startX,int startY,int endX,int endY){
		if(isInTheMiddleVertically(startY))
			if((isToTheLeft(startX) && isToTheRight(endX))||(isToTheLeft(endX) && isToTheRight(startX)))
				return true;
		return false;		
	}

	// returns true if the position is aligned with the wall vertically
	// at the same height
	private boolean isInTheMiddleVertically(int posY){
		boolean centered = (posY <= startPosY) && (posY >= endPosY);
		boolean centeredWallInverted = (posY <= endPosY) && (posY >= startPosY);

		return (centered || centeredWallInverted);
	}

	// returns true if the position is aligned with the wall horizontally
	// at the same width
	private boolean isInTheMiddleHorizontally(int posX){
		boolean centered = (posX <= startPosX) && (posX >= endPosX);
		boolean centeredWallInverted = (posX <= endPosX) && (posX >= startPosX);

		return (centered || centeredWallInverted);
	}	

	// returns true if the position is to the above the wall
	private boolean isAbove(int posY){
		return ( (posY <= startPosY) && (posY <= endPosY) );
	}	

	// returns true if the position is to the below the wall
	private boolean isBelow(int posY){
		return ( (posY >= startPosY) && (posY >= endPosY) );		
	}
	
	// returns true if the position is to the left from the wall
	private boolean isToTheLeft(int posX){
		return ( (posX <= startPosX) && (posX <= endPosX) );
	}

	// returns true if the position is to the right from the wall
	private boolean isToTheRight(int posX){
		return ( (posX >= startPosX) && (posX >= endPosX) );
	}	
	
	// draws a wall (4 testing!!!)
	public void draw(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		g2.setStroke(new BasicStroke(3));
		g.setColor(color);

		g2.draw(new Line2D.Float((float)startPosX, (float)startPosY, (float)endPosX, (float)endPosY));	
	}
}
